package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Course;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CourseQuery;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICourseService;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/courseorder")
public class CourseController {

    @Autowired
    private ICourseService courseService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CourseQuery qo){
        PageInfo pageInfo = courseService.query(qo);
        model.addAttribute("result", pageInfo);
        return "course/list"; // /WEB-INF/views/department/potentialList.ftl
    }
    @RequestMapping("/input")
    public String input(Model model,Long id){
        if(id!=null){ //编辑
            Course course = courseService.select(id);
            model.addAttribute("course",course);
        }
        List<Customer> customers = customerService.listAll();
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.listAll();
        model.addAttribute("customers",customers);
        model.addAttribute("systemDictionaryItems",systemDictionaryItems);
        return "course/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String update(Course course , HttpServletResponse response){
        if(course.getId() != null){
            courseService.update(course);
        }else {
            courseService.insert(course);
        }

        return "redirect:/courseorder/list.do";

    }


    @RequestMapping("/delete")
    public String delete(Model model,Long id){
        if(id != null){
            courseService.delete(id);
        }
        return "course/list";
    }
}
